#Race and Congress
#Merge policy areas from CAP coding
#Created by Mac Lockhart
#Created November 2, 2023
#Edited November 2, 2023


library(lfe)
library(stargazer)
library(readr)
library(data.table)
library(dplyr)
library(readxl)
library(ggplot2)
library(ggpubr)
'%!in%' <- function(x,y)!('%in%'(x,y))

setwd(rprojroot::find_root(rprojroot::is_rstudio_project))
policy <- fread("Data/policy_outcomes_withissueareas_AM25.csv")

policy$white_r <- policy$race=="1"
policy$black_r <- policy$race=="2"
policy$latino_r <- policy$race=="3"
policy$asian_r <- policy$race=="4"
policy$other_r <- policy$race%in%c("5", "6", "7", "8", "9")

policy$protestant_r <- policy$relig=="1"
policy$catholic_r <- policy$relig=="2"
policy$jewish_r <- policy$relig=="3"
policy$muslim_r <- policy$relig=="4"
policy$non_religious_r <- policy$relig=="5"
policy$other_religion_r <- policy$relig%in%c("7", "8")

policy$first_quartile_r <- policy$income=="1"
policy$second_quartile_r <- policy$income=="2"
policy$third_quartile_r <- policy$income=="3"
policy$fourth_quartile_r <- policy$income=="4"

policy$highschool_r <- policy$edu %in% c("1", "2")
policy$somecollege_r <- policy$edu %in% c("3", "4")
policy$bachelors_r <- policy$edu %in% c("5")
policy$postgrad_r <- policy$edu %in% c("6")

policy <- subset(policy, outcome <= 1)

policy$pref_outcome <- ifelse(policy$became_law==policy$outcome, 1, 0)
policy$house_outcome <- ifelse(policy$passed_house==policy$outcome, 1, 0)
policy$senate_outcome <- ifelse(policy$passed_senate==policy$outcome, 1, 0)

#Turning 99s and 98s into NAs
policy <- policy %>%
  mutate(pid7 = ifelse(pid7>7, NA, pid7),
         ideo = ifelse(ideo>5, NA, ideo),
         edu = ifelse(edu>6, NA, edu),
         income = ifelse(income>4, NA, income),
         reg_voter = ifelse(reg_voter>1, NA, reg_voter)
  )

#cap_codes<-read_xlsx("Congressional Agenda/CES CAP Coding.xlsx")
cap_codes<-fread("Intermediate Data/Congressional Agenda/CES CAP Coding All Questions.csv")

cap_codes$vote <- paste0("rcv",cap_codes$data_num, "_r")
policy<-left_join(policy, cap_codes, b=c("year"="data_year", "vote" = "vote"))
rm(cap_codes)


whites<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(!is.na(Bill_topic))%>%
  filter(race==1 & year %in% c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2021, 2022))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill") + labs(title="Win rate among White respondents by issue (Dem pres)") +ylab("Proportion Winning")

blacks<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(!is.na(Bill_topic))%>%
  filter(race==2 & year %in% c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2021, 2022))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill")+ labs(title="Win rate among Black respondents by issue (Dem pres)") +ylab("Proportion Winning")

whites_r<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(!is.na(Bill_topic))%>%
  filter(race==1 & year %in% c(2006, 2007, 2008, 2017, 2018, 2019, 2020))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill") + labs(title="Win rate among White respondents by issue (Rep pres)") +ylab("Proportion Winning")

blacks_r<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(!is.na(Bill_topic))%>%
  filter(race==2 & year %in% c(2006, 2007, 2008, 2017, 2018, 2019, 2020))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill")+ labs(title="Win rate among Black respondents by issue (Rep pres)") +ylab("Proportion Winning")

ggarrange(whites, blacks, whites_r, blacks_r, ncol=2, nrow=2)
ggsave("Figures/winrates_byrace_byissue.png", width=30, height=15)


whites<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(Bill_topic %in%c("Civil Rights", "Government Operations", "Health", "Macroeconomics"))%>%
  filter(race==1 & year %in% c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2021, 2022))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill") + labs(title="Win rate among White respondents by issue (Dem pres)") +ylab("Proportion Winning")

blacks<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(Bill_topic %in%c("Civil Rights", "Government Operations", "Health", "Macroeconomics"))%>%
  filter(race==2 & year %in% c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2021, 2022))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill")+ labs(title="Win rate among Black respondents by issue (Dem pres)") +ylab("Proportion Winning")

whites_r<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(Bill_topic %in%c("Civil Rights", "Government Operations", "Health", "Macroeconomics"))%>%
  filter(race==1 & year %in% c(2006, 2007, 2008, 2017, 2018, 2019, 2020))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill") + labs(title="Win rate among White respondents by issue (Rep pres)") +ylab("Proportion Winning")

blacks_r<-policy%>%mutate(outcome=as.factor(outcome))%>%
  filter(Bill_topic %in%c("Civil Rights", "Government Operations", "Health", "Macroeconomics"))%>%
  filter(race==2 & year %in% c(2006, 2007, 2008, 2017, 2018, 2019, 2020))%>%
  ggplot(aes(x = Bill_topic,fill = outcome)) + 
  geom_bar(position = "fill")+ labs(title="Win rate among Black respondents by issue (Rep pres)") +ylab("Proportion Winning")

ggarrange(whites, blacks, whites_r, blacks_r, ncol=2, nrow=2)
ggsave("Figures/winrates_byrace_byissue_main.png", width=30, height=15)
